
desc_stats_func <- function(data, sample_txt, group_txt) {
  t <- data[, .(sample      = sample_txt,
                group       = group_txt,
                obs_num     = scales::comma(length(unique(obs_uniq_ident))),
                day_lived   = sum(ifelse(DMG_died_within_365d == "0",
                                         366,
                                         as.numeric(difftime(DMG_date_of_death_XX,
                                                             S_index_date_XX, 
                                                             units = "days"))+1)),
                age_avg     = scales::comma(round(mean(DMG_age), 1)),
                female_pct  = mean(DMG_gender == "F"),
                arabs_pct   = mean(DMG_clinic_ethnicity == "arab"),
                immgrnt_pct = mean(DMG_sw_immigrant == "1"),
                yr_since_im = scales::comma(round(mean(DMG_years_from_immigration, 
                                                       na.rm = T), 1)),
                confined_pct= mean(COV_sw_confined == "1"),
                curr_smk_pct= mean(COV_smoking_code == "3"),
                low_ses_pct = mean(DMG_ses_by_clinic == "1"),
                med_ses_pct = mean(DMG_ses_by_clinic == "2"),
                hig_ses_pct = mean(DMG_ses_by_clinic == "3"),
                sup_ins_pct = mean(DMG_supplementary_insurance == "1"),
                MR_1m_pct   = mean(DMG_died_within_30d == "1"),
                MR_1y_pct   = mean(DMG_died_within_365d == "1"),
                MR_3y_pct   = mean(DMG_died_within_1095d == "1"),
                cost_1yrBef = scales::comma(round(mean(UTL_l365d_total_cost/12), 1)), 
                adm_l1y_pct = mean(UTL_l365d_diff_count > 0 | 
                                     UTL_l365d_hospPlanned_count > 0 |
                                     UTL_l365d_hospUnplanned_count > 0),
                cost_1yrAft = scales::comma(round(mean(UTL_f365d_total_cost/12), 1)),
                ave_cost_future_365d = scales::comma(round(sum(UTL_f365d_total_cost)/
                                                             (sum(ifelse(DMG_died_within_365d == "0",
                                                                         366,
                                                                         as.numeric(difftime(DMG_date_of_death_XX,
                                                                                             S_index_date_XX, 
                                                                                             units = "days"))+1)))*366/12,1)),
                adm_f1y_pct = mean(UTL_f365d_diff_count > 0 | 
                                     UTL_f365d_hospPlanned_count > 0 |
                                     UTL_f365d_hospUnplanned_count > 0)
  )]
  
  num_to_pct <- grep("pct", names(t), value=T)
  t[, (num_to_pct) := lapply(.SD,  
                             function(x) {(round(x*100, 1))}),  .SDcols = num_to_pct]
}


do_table_1 <- function(data_all =  dt_all,
                       data_cnr = dt_cnr,
                       min_age) {
  
  unif_desc_stats<- rbind(
    desc_stats_func(data_cnr[DMG_age>=min_age], 
                    "Cancer", "All"),
    desc_stats_func(data_cnr[DMG_age>=min_age & DMG_died_within_365d == "1"], 
                    "Cancer", "Decedent"),
    desc_stats_func(data_cnr[DMG_age>=min_age & DMG_died_within_365d == "0"], 
                    "Cancer", "Survivor"),
    desc_stats_func(data_all[DMG_age>=min_age], 
                    "All", "All"),
    desc_stats_func(data_all[DMG_age>=min_age & DMG_died_within_365d == "1"], 
                    "All", "Decedent"),
    desc_stats_func(data_all[DMG_age>=min_age & DMG_died_within_365d == "0"], 
                    "All", "Survivor")
  )
  
  unif_desc_stats[, day_lived_pct := day_lived/(sum(day_lived)/2), by = .(sample)]
  unif_desc_stats[, day_lived_pct := (round(day_lived_pct*100, 1))]
  unif_desc_stats[, day_lived := NULL]
  
  unif_desc_stats_t <- 
    as.data.table(t(unif_desc_stats),keep.rownames = T)
  
  names(unif_desc_stats_t)<- paste0(unif_desc_stats_t[1,],
                                    "_",
                                    unif_desc_stats_t[2,])
  
  # drop uninformative cells 
  unif_desc_stats_t[sample_group=="MR_1m_pct",
                    `:=` (All_Survivor = "NA", Cancer_Survivor = "NA")]
  unif_desc_stats_t[sample_group=="MR_3y_pct",
                    `:=` (All_Decedent = "NA", Cancer_Decedent = "NA")]
  unif_desc_stats_t[sample_group == "day_lived_pct",
                    `:=` (All_All = "NA", Cancer_All = "NA")]
  
  rows_to_keep_table1<- c( "age_avg" ,  "female_pct" ,  "hig_ses_pct" , "sup_ins_pct", 
                           "MR_1m_pct" , "MR_1y_pct","MR_3y_pct" ,
                           "cost_1yrBef" ,"adm_l1y_pct" ,
                           "cost_1yrAft" , "ave_cost_future_365d" ,"adm_f1y_pct" ,
                           "obs_num"    )
  
  invisible(Hmisc::latex(
    unif_desc_stats_t[
      sample_group %in% rows_to_keep_table1
      ][
        , sample_group:=factor(sample_group, levels=rows_to_keep_table1) 
        ][order(sample_group)][,-1],
    file = paste0("FE_desc_stats_Age",min_age,"p.tex"),
    center = 'centering',
    n.cgroup = c(3, 3),
    cgroup   = c("Cancer Sample", "General Population Sample"),
    colheads = c("All", "Decedent", "Survivor", "All", "Decedent", "Survivor"),
    n.rgroup = c(4, 3, 2,3, 1),
    rgroup   = c("Characteristics",
                 "Mortality Rate",
                 "\\thead{Utilization\\\\12 Months Before Initial Prognosis}",
                 "12 Months After Initial Prognosis",
                 ""),
    rowname = c("Age (mean)", "Female (\\%)", "High Socioeconomic Status (\\%)", "Supplementary Insurance (\\%)"
                , "1 month (\\%)", "1 year (\\%)",  "3 years (\\%)",
                "Average Monthly Spending (NIS)", "Any admission (\\%)", 
                "Average Monthly Spending  (Unadjusted NIS)",
                "Average Monthly Spending  (Adjusted NIS)",
                "Any admission (\\%)",
                "Number of Beneficiaries"),
    rowlabel = "",  
    col.just = c(rep.int("r", 6)),
    extracolheads = c("(1)", "(2)", "(3)", "(4)","(5)","(6)"),
    na.blank = TRUE, 
    extracolsize = "normalsize"
  ))
  
  save_result<-unif_desc_stats_t[
    sample_group %in% rows_to_keep_table1
    ][
      , sample_group:=factor(sample_group, levels=rows_to_keep_table1) 
      ][order(sample_group)][,-1]
  
  write_csv(save_result, paste0("desc_stats_Age",min_age,"p.csv"))
  return(save_result)
}  

